package opensearch_remote

import (
	"context"
	"git.mycaigou.com/gfyx/common/http_client"
	"git.mycaigou.com/gfyx/common/simplecopier"
)

type openSearchRepo struct{}

const (
	GetRadarListUri              = "/bigdata/radar/get-list"
	GetRadarContactUri           = "/bigdata/contact/radar-contact"
	GetContactListUri            = "/bigdata/contact/recommend"
	GetDeveloperScoreUri         = "/bigdata/customer/get-customer-score"
	GetContactCooperationListUri = "/bigdata/contact/cooperation-list"
	GetCustomerListUri           = "/bigdata/customer/get-list"
	GetCustomerDetailUri         = "/bigdata/customer/detail"
	FuzzySearchUri               = "/bigdata/customer/fuzzy-search"
	GetProjectListUri            = "/bigdata/project-life-cycle/get-list"
	FuzzySearchProjectUri        = "/bigdata/project-life-cycle/fuzzy-search"
	SearchBidUri                 = "/bigdata/bid-life-cycle/get-list"
	FuzzySearchBidUri            = "/bigdata/bid-life-cycle/fuzzy-search"
	GetSaleListUri               = "/bigdata/project/get-list"
	GetMohurdProjectListUri      = "/bigdata/mohurd-project/get-list"
	GetTakeLandListUri           = "/bigdata/taken-land/get-list"
)

func NewOpenSearchRepository() OpenSearchRepositoryIface {
	return &openSearchRepo{}
}

func (r *openSearchRepo) GetRadarList(ctx context.Context, request *GetRadarListRequest) (*GetRadarListData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := GetRadarListResponse{}
	contactParamMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetRadarListUri, contactParamMap, map[string]string{}).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) GetRadarContact(ctx context.Context, request *GetRadarContactRequest) ([]*GetRadarContactData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := GetRadarContactResponse{}
	contactParamMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetRadarContactUri, contactParamMap, nil).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) GetContactList(ctx context.Context, request *GetContactListRequest) (*GetContactListData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := GetContactListResponse{}
	contactParamMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetContactListUri, contactParamMap, nil).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) GetDeveloperScore(ctx context.Context, request *GetCustomerScoreRequest) ([]*GetCustomerScoreData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := GetCustomerScoreResponse{}
	contactParamMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetDeveloperScoreUri, contactParamMap, nil).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) GetCooperationList(ctx context.Context, request *GetCooperationListRequest) (*GetCooperationListData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := GetCooperationListResponse{}
	contactParamMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetContactCooperationListUri, contactParamMap, nil).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) GetCustomerList(ctx context.Context, request *GetCustomerListRequest) (*GetCustomerListData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := GetCustomerListResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetCustomerListUri, paramMap, nil).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) GetCustomerDetail(ctx context.Context, request *GetCustomerDetailRequest) ([]*CustomerDetail, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := GetCustomerDetailResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Get(ctx, GetCustomerDetailUri, paramMap).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) FuzzySearch(ctx context.Context, request *FuzzySearchRequest) ([]*FuzzySearchItem, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := FuzzySearchResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, FuzzySearchUri, paramMap, map[string]string{}).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) GetProjectList(ctx context.Context, request *GetProjectListRequest) (*GetProjectListData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := GetProjectListResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetProjectListUri, paramMap, map[string]string{}).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) FuzzySearchProject(ctx context.Context, request *FuzzySearchProjectRequest) ([]*FuzzySearchProjectItem, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := FuzzySearchProjectResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, FuzzySearchProjectUri, paramMap, map[string]string{}).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) SearchBid(ctx context.Context, request *SearchBidRequest) (*SearchBidData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := SearchBidResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, SearchBidUri, paramMap, map[string]string{}).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) FuzzySearchBid(ctx context.Context, request *FuzzySearchBidRequest) ([]*FuzzySearchBidItem, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := FuzzySearchBidResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, FuzzySearchBidUri, paramMap, map[string]string{}).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) SaleList(ctx context.Context, request *SaleListRequest) (*SaleListData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := SaleListResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetSaleListUri, paramMap, map[string]string{}).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) BuildList(ctx context.Context, request *BuildListRequest) (*BuildListData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := BuildListResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetMohurdProjectListUri, paramMap, map[string]string{}).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}

func (r *openSearchRepo) TakeLandList(ctx context.Context, request *TakeLandListRequest) (*TakeLandListData, error) {
	client := http_client.NewOpenSearchHttpClient()
	response := TakeLandListResponse{}
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetTakeLandListUri, paramMap, map[string]string{}).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response.Data, nil
}
